//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PricingPeriod.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Experimental/Commodities/Quantity.h>
#include <gen/QL/Experimental/Commodities/DateInterval.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (DateTime startDate, DateTime endDate, DateTime paymentDate, Cephei::QL::Experimental::Commodities::IQuantity^ quantity) : CDateInterval(CPricingPeriod::typeid)
{
    CQuantity^ _Cquantity;
    try
    {
#ifdef HANDLE
        _phPricingPeriod = NULL;
#endif
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //d
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //d
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate); //d
        _Cquantity = safe_cast<CQuantity^> (quantity);
        _Cquantity->Lock();
        QuantLib::Quantity& _quantity = static_cast<QuantLib::Quantity&> (_Cquantity->GetReference ()); 
        _ppPricingPeriod = new boost::shared_ptr<QuantLib::PricingPeriod> (new QuantLib::PricingPeriod ( _startDate,  _endDate,  _paymentDate,  _quantity ));
        SetDateInterval (boost::dynamic_pointer_cast<QuantLib::DateInterval> (*_ppPricingPeriod));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cquantity != nullptr) _Cquantity->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (boost::shared_ptr<QuantLib::PricingPeriod>& childNative, Object^ owner) : CDateInterval(CPricingPeriod::typeid)
{
#ifdef HANDLE
	_phPricingPeriod = NULL;
#endif
	_ppPricingPeriod = &childNative;
    _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (boost::dynamic_pointer_cast<QuantLib::DateInterval> (*_ppPricingPeriod));
}
Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (QuantLib::PricingPeriod& childNative, Object^ owner) : CDateInterval(CPricingPeriod::typeid)
{
#ifdef HANDLE
	_phPricingPeriod = NULL;
#endif
	_ppPricingPeriod = new boost::shared_ptr<QuantLib::PricingPeriod> (&childNative);
    _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (boost::dynamic_pointer_cast<QuantLib::DateInterval> (*_ppPricingPeriod));
    _PricingPeriodOwner = owner;
    _DateIntervalOwner = owner;
}

Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (CPricingPeriod^ copy) : CDateInterval(CPricingPeriod::typeid)
{
#ifdef HANDLE
	_phPricingPeriod = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPricingPeriod = new boost::shared_ptr<QuantLib::PricingPeriod> (copy->GetShared());
        _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (boost::dynamic_pointer_cast<QuantLib::DateInterval> (*_ppPricingPeriod));
    }
}
Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (PLATFORM::Type^ t) : CDateInterval(CPricingPeriod::typeid)
{
#ifdef HANDLE
	_phPricingPeriod = NULL;
#endif
	if (!t->IsSubclassOf(CPricingPeriod::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (QuantLib::Handle<QuantLib::PricingPeriod>& childNative, Object^ owner)  : CDateInterval(CPricingPeriod::typeid)
{
	_phPricingPeriod = &childNative;
	_ppPricingPeriod = &static_cast<boost::shared_ptr<QuantLib::PricingPeriod>>(childNative.currentLink());
    _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (boost::dynamic_pointer_cast<QuantLib::DateInterval> (*_ppPricingPeriod));
    _PricingPeriodOwner = owner;
}
Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (QuantLib::Handle<QuantLib::PricingPeriod> childNative)  : CDateInterval(CPricingPeriod::typeid)
{
	_phPricingPeriod = &childNative;
	_ppPricingPeriod = &static_cast<boost::shared_ptr<QuantLib::PricingPeriod>>(childNative.currentLink());
    _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (boost::dynamic_pointer_cast<QuantLib::DateInterval> (*_ppPricingPeriod));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CPricingPeriod::CPricingPeriod (QuantLib::PricingPeriod childNative)  : CDateInterval(CPricingPeriod::typeid)
{
#ifdef HANDLE
	_phPricingPeriod = NULL;
#endif
	_ppPricingPeriod = new boost::shared_ptr<QuantLib::PricingPeriod> (new QuantLib::PricingPeriod (childNative));
    _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (boost::dynamic_pointer_cast<QuantLib::DateInterval> (*_ppPricingPeriod));
}
#endif

Cephei::QL::Experimental::Commodities::CPricingPeriod::~CPricingPeriod ()
{
    if (_ppPricingPeriod != NULL)
    {
	    delete _ppPricingPeriod;
        _ppPricingPeriod = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CPricingPeriod::!CPricingPeriod ()
{
    if (_ppPricingPeriod != NULL)
    {
	    delete _ppPricingPeriod;
    }
}
QuantLib::PricingPeriod& Cephei::QL::Experimental::Commodities::CPricingPeriod::GetReference ()
{
    if (_ppPricingPeriod == NULL) throw REFNEW NativeNullException ();
	return **_ppPricingPeriod;
}
boost::shared_ptr<QuantLib::PricingPeriod>& Cephei::QL::Experimental::Commodities::CPricingPeriod::GetShared ()
{
    if (_ppPricingPeriod == NULL) throw REFNEW NativeNullException ();
	return *_ppPricingPeriod;
}
QuantLib::PricingPeriod* Cephei::QL::Experimental::Commodities::CPricingPeriod::GetPointer ()
{
    if (_ppPricingPeriod == NULL) throw REFNEW NativeNullException ();
	return &**_ppPricingPeriod;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PricingPeriod>& Cephei::QL::Experimental::Commodities::CPricingPeriod::GetHandle ()
{
	if (_phPricingPeriod == NULL)
	{
		_phPricingPeriod = new Handle<QuantLib::PricingPeriod> (*_ppPricingPeriod);
	}
	return *_phPricingPeriod;
}
#endif
bool Cephei::QL::Experimental::Commodities::CPricingPeriod::HasNative () 
{
	return (_ppPricingPeriod != NULL);
}

DateTime Cephei::QL::Experimental::Commodities::CPricingPeriod::PaymentDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppPricingPeriod)->paymentDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IQuantity^ Cephei::QL::Experimental::Commodities::CPricingPeriod::Quantity::get ()
{
    try
    {
    	QuantLib::Quantity& _rv = (QuantLib::Quantity&)(*_ppPricingPeriod)->quantity ( );   
        Cephei::QL::Experimental::Commodities::CQuantity^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CQuantity (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CPricingPeriodVector::CPricingPeriodVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Experimental::Commodities::CPricingPeriodVector::~CPricingPeriodVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Experimental::Commodities::CPricingPeriodVector::!CPricingPeriodVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CPricingPeriodVector::CPricingPeriodVector (std::vector<QuantLib::PricingPeriod>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Experimental::Commodities::CPricingPeriodVector::CPricingPeriodVector (std::vector<QuantLib::PricingPeriod> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::PricingPeriod> (vec);
}
#endif
Cephei::QL::Experimental::Commodities::CPricingPeriodVector::CPricingPeriodVector (std::vector<boost::shared_ptr<QuantLib::PricingPeriod>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CPricingPeriodVector::CPricingPeriodVector (std::vector<QuantLib::Handle<QuantLib::PricingPeriod>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Experimental::Commodities::CPricingPeriodVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Experimental::Commodities::IPricingPeriod^ Cephei::QL::Experimental::Commodities::CPricingPeriodVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::PricingPeriod& native = (*_pVValue)[index];
		return REFNEW CPricingPeriod (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::PricingPeriod& native = *(*_pVBoost)[index];
		return REFNEW CPricingPeriod (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::PricingPeriod& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CPricingPeriod (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Experimental::Commodities::CPricingPeriodVector::default::set (int index, Cephei::QL::Experimental::Commodities::IPricingPeriod^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CPricingPeriod^>(value)->GetReference ());
		}
        else
        {
            QuantLib::PricingPeriod& c = (*_pVValue)[index];
            c = dynamic_cast<CPricingPeriod^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CPricingPeriod^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::PricingPeriod>& c = (*_pVBoost)[index];
		c = dynamic_cast<CPricingPeriod^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::PricingPeriod> ());
		}
		QuantLib::Handle<QuantLib::PricingPeriod>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::PricingPeriod>& b = static_cast<boost::shared_ptr<QuantLib::PricingPeriod>>(c.currentLink());
		b = dynamic_cast<CPricingPeriod^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^ Cephei::QL::Experimental::Commodities::CPricingPeriodVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<IPricingPeriod^> (dynamic_cast<IPricingPeriod^>(default[index]));
}

void Cephei::QL::Experimental::Commodities::CPricingPeriodVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^ value) 
{
	default[index] = safe_cast<IPricingPeriod^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::PricingPeriod>& Cephei::QL::Experimental::Commodities::CPricingPeriodVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::PricingPeriod>>& Cephei::QL::Experimental::Commodities::CPricingPeriodVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::PricingPeriod>>& Cephei::QL::Experimental::Commodities::CPricingPeriodVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Experimental::Commodities::CPricingPeriodVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::PricingPeriod> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::PricingPeriod>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::PricingPeriod>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::IPricingPeriod^ Cephei::QL::Experimental::Commodities::CPricingPeriod_Factory::Create (DateTime startDate, DateTime endDate, DateTime paymentDate, Cephei::QL::Experimental::Commodities::IQuantity^ quantity)
{
    return REFNEW CPricingPeriod ( startDate,  endDate,  paymentDate,  quantity);
}
/* <summary>Create a single dimension array of PricingPeriod that can be passed into other function </summary> */
IVector<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^ Cephei::QL::Experimental::Commodities::CPricingPeriod_Factory::CreateVector ()
 {
	 return REFNEW CoVector<IPricingPeriod^> (REFNEW CPricingPeriodVector ());
 }
IVector<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^ Cephei::QL::Experimental::Commodities::CPricingPeriod_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<IPricingPeriod^>^ source)
 {
	 return REFNEW CoVector<IPricingPeriod^> (source, REFNEW CPricingPeriodVector ());
 }
/* <summary>Create a single dimension array of PricingPeriod with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^>^>^ Cephei::QL::Experimental::Commodities::CPricingPeriod_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^>^> (REFNEW CoCellVector<Cephei::QL::Experimental::Commodities::IPricingPeriod^> (REFNEW CPricingPeriodVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^>^>^ Cephei::QL::Experimental::Commodities::CPricingPeriod_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Commodities::IPricingPeriod^>^>^> (REFNEW CoCellVector<Cephei::QL::Experimental::Commodities::IPricingPeriod^> (source, REFNEW CPricingPeriodVector ()));
 }

